goog.provide('td.Map');

goog.require('Stefan.Rect');
goog.require('Stefan.Layer');
goog.require('td.Grid');
goog.require('td.FindWay');

td.Map = T.lang.createClass(function(config) {
	this.config = config = config || {};
	this.bg = config.background || 'img/ground.gif';
    this.type = config.type; 		//main|select
    this.columns = config.columns;	//列数
    this.rows =  config.rows;		//行数
    this.gridSize = config.gridSize;	//单元格宽高
    this.pX = this.config.x || 50;
    this.pY = this.config.y || 50;
    this.entrancePosition = this.config.entrancePosition || [0, 0]; 					//地图入口
    this.exitPosition = this.config.exitPosition || [this.columns - 1, this.rows - 1];	//地图出口
	this.width = this.columns * this.gridSize;	//地图宽度
	this.height = this.rows * this.gridSize;	//地图高度
	
	Stefan.Layer.call(this, document.body, {
		width: this.width,
		height: this.height,
		left: this.pX,
		top: this.pY
	});
	
	
	this.grids = [];		//单元格 hash
	this.armyArr = [];		//敌人hash
	this.bulletArr = [];	//子弹hash
	
	this.background = new Stefan.Rect();
	this.background.setSize(this.width, this.height).setFill(this.bg);	//设置位置is.setPosition(this.pX, this.pY).setSize(this.width, this.height);is.appendChild(this.background);
	this.appendChild(this.background);
	this.initGrids();
	
	
	this.on('mousemove', function(event){
		if(this.type == 'control') return;
		var position = event.position;
		var grid = this.getGridByPosition(position.x, position.y);
		if(grid)
			grid.onOver();
	});
	
	this.on('mousedown', function(event){
		if(event.nativeEvent.button !== 0) return;
		if(this.type == 'control') return;
		var position = event.position;
		var grid = this.getGridByPosition(arguments[0].position.x, arguments[0].position.y);
		grid.onClick();
	});
}, {
	superClass: Stefan.Layer
}).extend({
	initGrids: function() {
		for(var i = 0; i < this.rows; i++){
			for(var j = 0; j < this.columns; j++){
				var grid = new td.Grid({
					map: this,
					x: j * this.gridSize + this.gridSize / 2,
					y: i * this.gridSize + this.gridSize / 2,
					isEntrance: j == this.entrancePosition[0] && i == this.entrancePosition[1],
					isExit: j == this.exitPosition[0] && i == this.exitPosition[1],
					color: (i+j)%2?'rgba(0,0,0,.1)':'rgba(0,0,0,.2)',
					passable: true
				});
				this.grids.push(grid);
				this.appendChild(grid);
			}
		}
	},
	
	getGridByPosition: function (x, y) {
		var x = Math.floor(x/td.GRIDSIZE);
		var y = Math.floor(y/td.GRIDSIZE);
		return this.getGridByCoor(x, y);
	},

	getGridByCoor: function(x, y){
		return this.grids[y * this.columns + x];
	},

	ifBlock: function(grid){
		if (grid.isEntrance || grid.isExit) return true;
		var __this = this;
		var gridX = Math.floor(grid.pX/td.GRIDSIZE);
		var gridY = Math.floor(grid.pY/td.GRIDSIZE);
		fw = new td.FindWay(
			this.rows, this.columns,
			this.entrancePosition[0], this.entrancePosition[1],
			this.exitPosition[0], this.exitPosition[1],
			function (x, y) {
				var currentGrid = __this.getGridByCoor(x, y);
				return !(x == gridX && y == gridY) && !currentGrid.building && currentGrid.passable;
			}
		);
		return fw.is_blocked;
	}
});
